package com.example.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: ChenBin
 * @Date: 2018/6/13/0013 17:11
 */
public class ConfigUtil {
    private static final String CONFIG_FILENAME = "/code-config.properties";

    private static final String DEFAULT_CHAR_SET = "UTF-8";

    private static class ConfigHolder {
        private static final ConfigUtil CONFIG = new ConfigUtil();
    }

    private static Map<String, String> context = new HashMap<>();

    static {
        Properties properties = new Properties();
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(ConfigUtil.class.getResourceAsStream(CONFIG_FILENAME), DEFAULT_CHAR_SET));
            properties.load(in);
        } catch (Exception e) {
            throw new RuntimeException("Can not find config file：" + CONFIG_FILENAME, e);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        put(properties);
    }

    public static ConfigUtil getConfig() {
        return ConfigHolder.CONFIG;
    }

    public String get(String key) {
        return context.get(key);
    }

    private static void put(Properties properties) {
        for (Object key : properties.keySet()) {
            context.put((String) key, evaluate((String) key, properties));
        }
    }

    public int getInt(String key) {
        return Integer.parseInt(context.get(key));
    }

    private static String evaluate(String key, Properties properties) {
        String value = (String) properties.get(key);
        if (value == null) {
            throw new RuntimeException("Key [" + key + "] is undefined");
        }

        value = value.trim();

        StringBuilder sb = new StringBuilder();

        int index = 0;
        int beginIndex;
        int endIndex;
        for (; ; ) {
            beginIndex = value.indexOf("${", index);
            endIndex = value.indexOf("}", beginIndex);
            if (beginIndex == -1 || endIndex == -1) {
                sb.append(value.substring(index));
                break;
            } else {
                sb.append(value.substring(index, beginIndex));
                sb.append(evaluate(value.substring(beginIndex + 2, endIndex), properties));
                index = endIndex + 1;
                if (index >= value.length()) {
                    break;
                }
            }
        }
        return sb.toString();
    }

    private ConfigUtil() {
    }

    /**
     * 转换为驼峰
     *
     * @param underscoreName
     * @return
     */
    public static String camelCaseName(String underscoreName) {
        StringBuilder result = new StringBuilder();
        if (underscoreName != null && underscoreName.length() > 0) {
            boolean flag = false;
            for (int i = 0; i < underscoreName.length(); i++) {
                char ch = underscoreName.charAt(i);
                if ("_".charAt(0) == ch) {
                    flag = true;
                } else {
                    if (flag) {
                        result.append(Character.toUpperCase(ch));
                        flag = false;
                    } else {
                        result.append(ch);
                    }
                }
            }
        }
        return result.toString();
    }


    /**
     * Description: 过滤字段
     * @author: 王培喜(58528792@163.com)
     * @date:  2021/9/7 10:13
     */
    public static String filterTableName(String tableName,String filterName){
        //过滤字段为空 不过滤
        if(null == filterName || filterName.isEmpty()){
            return null;
        }
        String[] split = filterName.toLowerCase().trim().split(",");
        for (String s : split) {
            if(tableName.contains(s)){
                return s;
            }
        }
        return null;
    }

    /**
     * 根据数据表字段类型转换为对应封装的JAVA类型(适用于Oracle)
     */
    public static String switchObjectByOracle(String column) {
        String resultObj = "";
        String dataType = column.substring(0, 1);
        String dataType2 = column.substring(0, 2);
        if (dataType.equalsIgnoreCase("N") || column.toLowerCase().contains("bigint")) {//NUMBER number
            resultObj = "Long";
            return resultObj;
        }

        if (dataType.equalsIgnoreCase("I")  || column.toLowerCase().contains("tinyint")) {//INT
            resultObj = "Integer";
            return resultObj;
        }

        if (dataType.equalsIgnoreCase("V")  || column.toLowerCase().contains("char")) {///CLOB VARCHAR varchar
            resultObj = "String";
            return resultObj;
        }

        if (dataType.equalsIgnoreCase("F") || column.toLowerCase().contains("float")) {//FLOAT
            resultObj = "Float";
            return resultObj;
        }

        if (dataType2.equalsIgnoreCase("do") || column.toLowerCase().contains("double")) { //Double
            resultObj = "Double";
            return resultObj;
        }

        if (dataType2.equalsIgnoreCase("DA")  || column.toLowerCase().contains("time")) {//DATE date
            resultObj = "Date";
            return resultObj;
        }
        if (column.toLowerCase().contains("decimal")) {//decimal BigDecimal
            resultObj = "BigDecimal";
            return resultObj;
        }

        return resultObj;
    }
}
